package com.sgu6.sgu6_machine.ui.activity;

import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.util.Log;
import android.view.MenuItem;

import com.sgu6.middleware.manager.ThreadManager;
import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.adapter.LogAdapter;
import com.sgu6.sgu6_machine.base.BaseA;
import com.sgu6.sgu6_machine.common.Constant;
import com.sgu6.sgu6_machine.config.MachineConfig;
import com.sgu6.sgu6_machine.container.MachineContainer;
import com.sgu6.sgu6_machine.util.HttpUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class UploadLogA extends BaseA implements LogAdapter.OnItemLogClickListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rvLogs)
    RecyclerView rvLogs;
    private AlertDialog.Builder builder;

    private List<String> logsName = new ArrayList<>();
    private LogAdapter logAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_log);
        ButterKnife.bind(this);

        initUI();

        logAdapter = new LogAdapter(this, logsName);
        logAdapter.setOnItemLogClickListener(this);
        rvLogs.setAdapter(logAdapter);
        rvLogs.setLayoutManager(new LinearLayoutManager(this));

        try {
            File file = new File(Constant.FILE_ROOT_PATH + "/sgu6/logs/");
            String[] files = file.list();
            logsName.addAll(Arrays.asList(files));
            logAdapter.notifyDataSetChanged();
        } catch (Exception e) {
            Log.i(TAG, "onCreate: " + e.getLocalizedMessage());
        }

    }

    @Override
    public void initUI() {
        initToolbar();

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return true;
    }


    private void initToolbar() {
        toolbar.setTitle("日志上传");
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setBackgroundResource(R.color.colorPrimaryDark);
        setSupportActionBar(toolbar);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(true);
    }

    @Override
    public void onItemLogClick(int position) {
        builder = new AlertDialog.Builder(UploadLogA.this);
        builder.setTitle("提示");
        builder.setMessage("是否上传此文件?");
        builder.setNegativeButton("取消", (dialog, which) -> {
        });

        builder.setPositiveButton("确定", (dialog, which) -> {
            ThreadManager.getThreadPollProxy().execute(() -> HttpUtils.uploadLogFile(UploadLogA.this, MachineContainer.machineConfigDto.machineNo,logsName.get(position)));
        });
        builder.show();
    }
}
